package top.pengdong.pictureShare.file.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域请求拦截
 * Created by LT on 2019/3/31.
 */
// @Component
public class CsrFilter implements Filter {

    private final static Logger logger = LoggerFactory.getLogger(CsrFilter.class);

    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) resp;
        logger.info("CsrFilter-------------ip>>" + req.getRemoteAddr());
        // 内网使用，跨越请求暂不添加测试，如果允许多个域请求数据的话，可以直接用逗号隔开："http://www.baidu.com,http://google.com"
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept,X-Requested-With");
        chain.doFilter(req, resp);

    }

    public void init(FilterConfig filterConfig) { }

    public void destroy() { }
}
